package com.woniu.job.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.job.entity.*;
import com.woniu.job.entity.vo.InterviewVO;
import com.woniu.job.mapper.*;
import com.woniu.job.service.IInterviewService;
import com.woniu.job.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author song
 * @since 2019-12-24
 */
@Service
@Transactional
public class InterviewServiceImpl extends ServiceImpl<InterviewMapper, Interview> implements IInterviewService {
    @Autowired
    private ResumeworkMapper resumeworkMapper;
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private InterviewMapper interviewMapper;

    /**
     * 循环添加面试邀请
     * @param interviewVO
     */
    @Override
    public void add(InterviewVO interviewVO) {
        List<String> ids =  interviewVO.getResumeIds();
        for(String id:ids){
            Interview interview = new Interview();
            Resumework resumework = resumeworkMapper.selectById(id);
            String resumeId = resumework.getResumeId();
            Resume resume = resumeMapper.selectById(resumeId);
            String workId = resumework.getWorkId();
            Work work = workMapper.selectById(workId);
            Company company = companyMapper.selectById(work.getCompanyId());
            interview.setId(Utils.getUID());
            interview.setUserId(resume.getUserId());
            interview.setCompanyId(work.getCompanyId());
            interview.setCompanyName(company.getName());
            interview.setWorkId(workId);
            interview.setWorkName(work.getName());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            LocalDateTime time = LocalDateTime.parse(interviewVO.getTime(),formatter);
            interview.setTime(time);
            interview.setAddress(interview.getAddress());
            interview.setStatus("5");
            interviewMapper.insert(interview);
        }
    }

    @Override
    public void addOne(Interview interview) {
        interview.setId(Utils.getUID());
        Work work = workMapper.selectById(interview.getWorkId());
        interview.setWorkName(work.getName());
        Company company = companyMapper.selectById(interview.getCompanyId());
        interview.setCompanyName(company.getName());
        interview.setStatus("5");
        interviewMapper.insert(interview);
    }

    @Override
    public PageBean<Interview> list(String companyId,Integer page) {
        int pageSize = 2;
        Page p = PageHelper.startPage(page,pageSize);
        PageBean<Interview> resumeworkPageBean = new PageBean<>();
        resumeworkPageBean.setData(interviewMapper.list(companyId));
        resumeworkPageBean.setCurrPage(page);
        resumeworkPageBean.setTotalPage(p.getPages());
        resumeworkPageBean.setCurrNum(p.getPageSize());
        resumeworkPageBean.setTotalNum((int)p.getTotal());
        resumeworkPageBean.setPageSize(pageSize);
        return resumeworkPageBean;
    }


    /**
     * @Description: 修改面试状态
     * @Param: [面试信息id数组, 用户id1]
     * @return: void
     * @Author: 陈C
     * @Date: 2019/12/31
     */
    @Override
    public void updateStutas(String[] id, String id1) {
        for(int i = 0;i<id.length;i++){
            interviewMapper.updateStutas(id[i]);
        }
    }
}
